package com.hdoit.apollo.web.purchase.providers;

import java.util.ArrayList;
import java.util.List;

import com.hdoit.apollo.logical.purchase.model.Purchase;
import com.hdoit.apollo.logical.purchase.service.IPurchaseService;
import com.hdoit.apollo.logical.viewmodel.purchase.PurchaseSearch;
import com.tennetcn.core.common.message.ListItem;
import com.tennetcn.core.common.utils.CommonApplicationContextUtil;
import com.tennetcn.web.component.attribute.provider.ISingleOptionListProvider;

public class PurchaseProvider implements ISingleOptionListProvider<ListItem>{

	IPurchaseService purchaseService=CommonApplicationContextUtil.getCurrentContext().getBean(IPurchaseService.class);
	
	private List<ListItem> dataList;

	@Override
	public List<ListItem> getDataList(Object viewData) {
		PurchaseSearch search =new PurchaseSearch();
		List<Purchase> purchases = purchaseService.queryListMP(search, null);
		
		if(purchases==null||purchases.size()<=0){
			return null;
		}
		dataList=new ArrayList<ListItem>();
		ListItem listItem=null;
		for (Purchase purchase : purchases) {
			listItem=new ListItem();
			listItem.setText(purchase.getPurchaseNo());
			listItem.setValue(purchase.getPurchaseId());
			
			dataList.add(listItem);
		}
		return dataList;
	}

	@Override
	public ListItem getSelectedItem(Object value, Object viewData) {
		if(value==null){return null;}
		if(dataList==null){return null;}
		for (ListItem listItem : dataList) {
			if(listItem.getValue().equals(value)){
				return listItem;
			}
		}
		return null;
	}

}
